<?php
class Tourism_goods_category_model extends CI_Model
{
    private $table = 'tourism_goods_category';
    
    public function total($getData)
    {
        if (!empty($getData['level'])) {
            $this->db->where('level', $getData['level']);
        }
        if (!empty($getData['name'])) {
            $this->db->like('name', $getData['name']);
        }
        $parent_id = array();/*所属分类*/
        if (!empty($getData['class_a'])) {
            $parent_id[] = $getData['class_a'];
        }
        if (!empty($getData['class_b'])) {
            $parent_id[] = $getData['class_b'];
        }
        if (!empty($parent_id)) {
            $this->db->where_in('parent_id', $parent_id);
        }
        if (!empty($getData['status'])) {
            $this->db->where('status', $getData['status']);
        }
        return $this->db->count_all_results($this->table);
    }
    
    public function page_list($page_num, $num, $getData)
    {
        if (!empty($getData['level'])) {
            $this->db->where('level', $getData['level']);
        }
        if (!empty($getData['name'])) {
            $this->db->like('name', $getData['name']);
        }
        $parent_id = array();/*所属分类*/
        if (!empty($getData['class_a'])) {
            $parent_id[] = $getData['class_a'];
        }
        if (!empty($getData['class_b'])) {
            $parent_id[] = $getData['class_b'];
        }
        if (!empty($parent_id)) {
            $this->db->where_in('parent_id', $parent_id);
        }
        if (!empty($getData['status'])) {
            $this->db->where('status', $getData['status']);
        }
        $this->db->order_by('id', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get($this->table);
    }
    
    public function getCategoryLevel($parent_id=0)
    {
        $this->db->where('parent_id', $parent_id);
        $result = $this->db->get($this->table);
        if ($result->num_rows() > 0){
            return $result->result_array();
        }
        return array();
    }
    
    /*
     * 验证是否是最后一级分类
     * */
    public function isUltimate($pid = 0)
    {
        $this->db->where('parent_id', $pid);
        return $this->db->count_all_results($this->table);
    }

    /*
     * 深度调用
     * */
    function getLists($pid = 0, $level = 3, $deep = 1, &$lists = array())
    {
        $result = $this->getCategoryByParentId($pid, $level);
        foreach ($result->result() as $item) {
            $item->name = str_repeat('&nbsp;&nbsp;&nbsp;', $deep) . '|---' . $item->name;
            $lists[] = $item;
            if ($deep < $level) {
                $this->getLists($item->id, $level, ++$deep, $lists);
            }
            --$deep;
        }
        return $lists;
    }

    /*
     * 递归调用 输出 select
     * */
    public function displayLists($pid = 0, $pitch = 1, $type = 0, $level = 3)
    {
        $result = $this->getLists($pid, $level);
        $str = '<select name = "parent_id" autocomplete="off">';
        if ($type != 0) {
            $str .= '<option value = “０”>顶级分类</option>';
        }
        foreach ($result as $item) {
            $selected = "";
            if ($pitch == $item->id) {
                $selected = 'selected=selected';
            }
            $str .= '<option value = "' . $item->id . '"' . $selected . '>' . $item->name . '</option>';
        }
        return $str .= '</select>';
    }

     /*
     * 根据父类的id 获取类别数据
     * */
    public function getCategoryByParentId($pid, $level = 0)
    {
        if ($level) {
            $this->db->where('level <', $level);
        }
        $this->db->where('parent_id', $pid);
        return $this->db->get($this->table);
    }
    
     /**
     * 分类名称--店铺的所属分类
     * @param unknown $level
     * @param number $parent_id
     */
    public function getCategoryByReason($level,$parent_id){
    	
        $this->db->where('parent_id',$parent_id);
        $this->db->where('level',$level);
        $this->db->where('status',1);
        return $this->db->get($this->table);
    }

    public function insertCategory($postData)
    {
        $parent = array();
        if (!empty($postData['class_a'])) {
            $parent[] = $postData['class_a'];
            if (!empty($postData['class_b'])) {
                $parent[] = $postData['class_b'];
            }
        }
        $full_name = '';
        if (!empty($parent)) {
            $result = $this->tourism_goods_category->getByCategoryFullName($parent);
            foreach ($result->result() as $item) {
                $full_name .= $item->name.'>';
            }
        }
        $full_name = $full_name.$postData['name'];
        $parents = ',0,'.implode(',', $parent).',';
        
        $data = array(
            'name'      => $postData['name'],
            'parent_id' => end($parent),
            'status'    => 1,
            'level'     => $postData['level'],
            'parents'   => $parents,
            'full_name' => $full_name
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function getByCategoryFullName($parent)
    {
        $this->db->where_in('id', $parent);
        $this->db->order_by('id', 'ASC');
        return $this->db->get($this->table);
    }

    /*
    * 插入类别数据
    * */
    public function getLevel($id)
    {
        $this->db->select('level');
        $this->db->where('id', $id);
        $result = $this->db->get($this->table)->row();
        return isset($result->level) ? $result->level : 0;
    }

     /*
     * 更新数据数据
     * */
    public function updateCategory($postData)
    {
        $parent = array();
        if (!empty($postData['class_a'])) {
            $parent[] = $postData['class_a'];
            if (!empty($postData['class_b'])) {
                $parent[] = $postData['class_b'];
            }
        }
        $full_name = '';
        if (!empty($parent)) {
            $result = $this->tourism_goods_category->getByCategoryFullName($parent);
            foreach ($result->result() as $item) {
                $full_name .= $item->name.'>';
            }
        }
        $full_name = $full_name.$postData['name'];
        $parents = ',0,'.implode(',', $parent).',';
        
        $data = array(
            'name'      => $postData['name'],
            'parent_id' => end($parent),
            'status'    => 1,
            'level'     => $postData['level'],
            'parents'   => $parents,
            'full_name' => $full_name
        );
        $this->db->where('id', $postData['id']);
        return $this->db->update($this->table, $data);

    }

     /**
     * 根据最后一级获取类别全称
     * */
    public function getCategoryByLastId($id, $str = array())
    {
        $this->db->where('id', (int)$id);
        $result = $this->db->get($this->table)->row();
        if(empty($result)){
            return '';
        }
        if ($result->parent_id == 0) {
            $str[] = $result->name;
            return implode('>', array_reverse($str));
        } elseif ($result->parent_id > 0) {
            $str[] = $result->name;
            return $this->getCategoryByLastId($result->parent_id, $str);
        } else {
            return '';
        }
    }

    /*
     * 获取其父类id的集合
     * */
    public function getParamsId($id, $str = array())
    {
        $this->db->where('id', $id);
        $result = $this->db->get($this->table)->row();
        if(empty($result)){
            return $str;
        }
        if ($result->parent_id == 0) {
            $str[] = $result->id;
            return $str;
        } elseif ($result->parent_id >= 0) {
            $str[] = $result->id;
            return $this->getParamsId($result->parent_id, $str);
        } else {
            return array();
        }
    }
    
    /*----------------------------------------------------------*/
    //根据id获取一条类别信息
    public function getCategoryById($id)
    {
        $this->db->where('id', $id);
        return $result = $this->db->get($this->table);

    }

    //获取其子类的ids
    function getChildrenIds($id)
    {
        $ids = '';
        $this->db->where('parent_id', $id);
        $fruit = $this->db->get($this->table);

        if ($fruit->result()) {
            foreach ($fruit->result() as $item) {
                $ids .= ',' . $item->id;
                $ids .= $this->getChildrenIds($item->id);
            }
        }
        return $ids;
    }

    //获取类别名称
    public function getCategoryNameById($id)
    {
        $this->db->select('name');
        $this->db->where('id', $id);
        $result = $this->db->get($this->table)->row_array();
        return $result['name'];
    }

    //获取父类id
    public function getParentId($childrenId)
    {
        $this->db->where('id', $childrenId);
        $result = $this->db->get($this->table)->row_array();
        if ($result['parent_id'] == 0) {
            return $result['id'];
        } else {
            return $this->getParentId($result['parent_id']);
        }
        return false;
    }
    
    /**
     * 删除分类
     * @param unknown $id
     */
    public function delete($id)
    {
        $this->db->where('id', $id);
        return $this->db->delete($this->table);
    }
}